package com.sss.support.poi.transform;

import java.io.File;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sss.gsb.orm.model.DeadMan;
import com.sss.support.poi.CellValueUtil;
import com.sss.support.poi.ModelCreator;
import com.sss.support.poi.ReaderRole;

public class DeadManModelTransformer extends ModelTransformer<DeadMan> {
	private static final Logger log = LoggerFactory
			.getLogger(DeadManModelTransformer.class);

	public DeadManModelTransformer(File excel, Integer start, Integer end) {
		this.setExcelFile(excel);
		this.setParserRole(new DeadManReaderRole(start, end));
		this.setCreator(new DeadManCreator());
	}
	private static class DeadManCreator implements ModelCreator<DeadMan> {

		private String provinceName;
		private static final String EMPTY_MARK = "-----";

		@Override
		public DeadMan create(Row r) throws Exception {
			try {
				
				// 0=no, 1=id,2=idCard,3=fullname,4=age,5=deathdate,6=deathreason				
				// do nothing when found the empty row
				if (isEmptyRow(r)) {
					return null;
				}

				if (isProvinceRow(r)) {					
					this.provinceName = r.getCell(3).getStringCellValue();
					log.info("[Group province name : {}]",this.provinceName);
				} else if (!StringUtils.isEmpty(this.provinceName)) {

					DeadMan model = new DeadMan();

					model.setIdCard(CellValueUtil.toStringValue(r.getCell(2)));

					model.setFullName(CellValueUtil.toStringValue(r.getCell(3)));
					
					model.setAge(Integer.valueOf((CellValueUtil.toStringValue(r.getCell(4)))));

					model.setDeadReason(CellValueUtil.toStringValue(r.getCell(6)));

					model.setProvince(provinceName);

					log.debug("[show row data : {}]", model.toString());
					return model;

				} else {
					throw new NullPointerException(
							"[Province name attribute cann't be null]");
				}

				return null;
			} catch (Exception e) {
				throw new Exception("[Creator exception occured !!]", e);
			}
		}

		private boolean isProvinceRow(Row r) {
			// columns 2 and 4 are equal 0 but column 3 is not empty
			if (CellValueUtil.isEmpty(r.getCell(2))
					|| CellValueUtil.isEmpty(r.getCell(4))
					&& !CellValueUtil.isEmpty(r.getCell(3))) {
				log.debug("[ is province row ? : {}]","true");
				return true;
			}
			return false;
		}

		private boolean isEmptyRow(Row r) {
			// cell 3 either is empty or has values equal "----"
			if (CellValueUtil.toStringValue(r.getCell(3)).equals(EMPTY_MARK)
					|| CellValueUtil.isEmpty(r.getCell(3))) {
				log.debug("[ is empty row ? : {}]","true");
				return true;
			}
			return false;
		}
	}

	private static class DeadManReaderRole implements ReaderRole {
		private Integer startRow;
		private Integer endRow;
		private Integer sheetNo;

		public DeadManReaderRole(Integer start, Integer end) {

			this(start, end, defaultSheetNo);
		}

		public DeadManReaderRole(Integer start, Integer end, Integer sheetNo) {
			this.startRow = start;
			this.endRow = end;
			this.sheetNo = sheetNo;
		}

		@Override
		public Integer getEndRow() {
			return this.endRow;
		}

		@Override
		public Integer getStartRow() {
			return this.startRow;
		}

		@Override
		public Integer getSheetNo() {
			return this.sheetNo;
		}

	}

}
